package com.example.demo.service.impl;


import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.example.demo.dao.UserDao;
import com.example.demo.entity.EtUser;
import com.example.demo.service.UserService;
import com.example.demo.util.EncoderHandler;
import com.example.demo.util.ResultPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


@Service
@Transactional(propagation = Propagation.REQUIRED)
public class UserServiceImpl implements UserService {

	@Resource
	private UserDao userDao;
   
    @Override
    public int login(String userId, String password, HttpServletRequest req) {
        int i = 1;

        EtUser user = userDao.getUserById(userId);
        if(user==null) {
        	return 0;
        }

        String passWordSalt = user.getPasswordSalt();
        String dbPassWord = user.getPassword();
        password = EncoderHandler.getSHA1(password + passWordSalt);

        if (!password.equals(dbPassWord)) {
            return 0;
        } else {
            req.getSession().setAttribute("etUser", user);
        }
        
        return i;

    }
    
    @Override
    public int register(String userId, String userPasswd, String userName,Integer isAdmin,String companyName,String phoneNum) {
    	EtUser user = userDao.getUserById(userId);
        if (user != null) {
        	return 2;
        }
        String passWordSalt = UUID.randomUUID().toString();
        String dbPassWord = "";
        dbPassWord = EncoderHandler.getSHA1(userPasswd + passWordSalt);
        EtUser etUser = new EtUser();
        etUser.setUserName(userName);
        etUser.setPassword(dbPassWord);
        etUser.setPasswordSalt(passWordSalt);
        etUser.setUserId(userId);
        etUser.setIsAdmin(isAdmin);
        etUser.setCompanyName(companyName);
        etUser.setPhoneNum(phoneNum);
        int i=userDao.register(etUser);
        
        return i;
    }

	@Override
	public ResultPage<EtUser> getUserList(EtUser user) {
		ResultPage<EtUser> page = new ResultPage<EtUser>();
		int bTablePageNum = (user.getOffset() / user.getLimit());
		PageHelper.startPage(bTablePageNum + 1, user.getLimit());
		List<EtUser> list = new ArrayList<>();
		list = userDao.getUserList(user);
		PageInfo<EtUser> table = new PageInfo<EtUser>(list);
		page.setTotal(table.getTotal());
		page.setRows(table.getList());
		return page;
	}

	@Override
	public EtUser getUserInfo(String userId) {
		EtUser user=userDao.getUserById(userId);
		if(user==null) {
			user=new EtUser();
		}else {
			user.setPassword("");
			user.setPasswordSalt("");
		}
		return user;
	}

	@Override
	public int updateUser(String userId, String userPasswd, String userName, Integer isAdmin) {

		EtUser oldUser=userDao.getUserById(userId);
		EtUser user=new EtUser();
		user.setUserId(userId);
		user.setUserName(userId);
		
        String passWordSalt = oldUser.getPasswordSalt();
        String newPassword = EncoderHandler.getSHA1(userPasswd + passWordSalt);
		user.setPassword(newPassword);
		user.setIsAdmin(isAdmin);
		int i=userDao.updateUser(user);
		return i;
		
	}

	@Override
	public void addUser(EtUser user) throws Exception {
		if(user.getUserId()!=null && !StringUtils.equals("", user.getUserId())){
			EtUser etUser = userDao.getUserById(user.getUserId());
			updateUser(user, etUser);
//			userDao.updateUser(etUser);
			userDao.updateWebUser(etUser);
			
		} else{
			user.setUserId(removeLine());
			user.setIsAdmin(EtUser.IS_NOT_ADMIN);
			userDao.addUser(user);
		}
		
		
	}

	
	private String removeLine(){
		String uuid = UUID.randomUUID().toString();
		
		return uuid.replace("-", "");
	}
	
	private void updateUser(EtUser dst, EtUser src){
		
		if(dst.getCompanyName()!=null){
			src.setCompanyName(dst.getCompanyName());
		}
		if(dst.getIsAdmin()!=null){
			src.setIsAdmin(dst.getIsAdmin());
		}
		
		if(dst.getUserName()!=null){
			src.setUserName(dst.getUserName());
		}
		if(dst.getPhoneNum()!=null){
			
			src.setPhoneNum(dst.getPhoneNum());
		}
		
	}

	@Override
	public ResultPage<EtUser> getWebUserList(EtUser user) {
		ResultPage<EtUser> page = new ResultPage<EtUser>();
		int bTablePageNum = (user.getOffset() / user.getLimit());
		PageHelper.startPage(bTablePageNum + 1, user.getLimit());
		List<EtUser> list = new ArrayList<>();
		list = userDao.getWebUserList(user);
		PageInfo<EtUser> table = new PageInfo<EtUser>(list);
		page.setTotal(table.getTotal());
		page.setRows(table.getList());
		return page;
	}

	@Override
	public EtUser getWebUserInfo(String userId) {
		EtUser user=userDao.getWebUserById(userId);
		if(user==null) {
			user=new EtUser();
		}else {
			user.setPassword("");
			user.setPasswordSalt("");
		}
		return user;
	}


}
